// CreateGeometricElement.java
// by Alexandre Martins, 2013
// MAC 5857 - Prof. Leonidas

package example.ilm.model;

import ilm.framework.assignment.model.DomainAction;
import ilm.framework.domain.DomainModel;

public class CreateGeometricElement extends DomainAction {

    private IlmDomainModel _domain;
    private GeometricElement geometricElement;
    private Workspace workspace;
    private static int cnt = 0;

    public CreateGeometricElement() {
        super("createGeometricElement[" + (++cnt) + "]", "created a geometric element");
    }

    public void setGeometricElement(GeometricElement geomel) {
        geometricElement = geomel;
    }
    
    public void setWorkspace(Workspace worksp) {
        workspace = worksp;
    }

    /**
     * Defines the DomainModel on which this action can happen. It must be
     * converted to IlmDomainModel so this class can call its specific methods.
     * 
     * @param an
     *            IlmDomainModel
     */
    public void setDomainModel(DomainModel model) {
        _domain = (IlmDomainModel) model;
    }

    /**
     * Method that is called when this action is executed.
     */
    protected void executeAction() {
        _domain.addGeometricElement(_currentState, geometricElement);
    }

    /**
     * Method that is called when this action is undone.
     */
    protected void undoAction() {
        _domain.removeGeometricElement(_currentState, workspace, geometricElement);
    }

    /**
     * Compares this to another DomainAction.
     */
    public boolean equals(DomainAction a) {
        CreateGeometricElement b = (CreateGeometricElement)a;
        return (getName().equals(a.getName()) && getDescription().equals(a.getDescription()) && geometricElement.equals(b.geometricElement));
    }

    /**
     * This method is used for converting this object into string
     * 
     * @see example.ilm.model.IlmDomainConverter
     */
    public String toString() {
        return "" + 
            "<creategeometricelement>" + 
                "<name>" + getName() + "</name>" + 
                "<description>" + getDescription() + "</description>" + 
                "<geometricelement>" + 
                    geometricElement.toString() + 
                "</geometricelement>" + 
            "</creategeometricelement>";
    }
}
